﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using System.Data.SqlClient;
using System.Data;

namespace BLL
{
    public class LOAICLSBLL
    {
        DataService DB = new DataService();

        public void insert(int maloai, string tenloai)
        {
            string sql = "insert into loaicls values (@maloai,@tenloai)";

            SqlParameter paraMaloai = new SqlParameter("maloai", maloai);
            SqlParameter paraTenloai = new SqlParameter("tenloai", tenloai);

            DB.ExecuteNonQuery(sql, paraMaloai, paraTenloai);
        }

        public void update(int maloai, string tenloai)
        {
            string sql = "update loaicls set tenloai = '" + tenloai.ToString() + " where maloai = " + maloai.ToString();
            DB.ExecuteNonQuery(sql);
        }

        public void delete(int maloai)
        {
            string sql = "delete from loaicls where maloai = " + maloai.ToString();
            DB.ExecuteNonQuery(sql);
        }

        public List<LOAICLS> getAll() {
            string sql = "select * from loaicls";

            DataTable table = new DataTable();
            table = DB.ExecuteReader(sql);

            List<LOAICLS> listLcls = new List<LOAICLS>();
            foreach(DataRow r in table.Rows){
                LOAICLS cls = new LOAICLS();
                cls.maloaicls = Int32.Parse( r[0].ToString());
                cls.tenloai = r[1].ToString();

                listLcls.Add(cls);
            }

            return listLcls;
        }

        public LOAICLS getLoaiCLSWithID(int idLoaiCls)
        {
            string sql = "select * from loaicls where maloai = '" + idLoaiCls.ToString() + "'";

            DataTable table = new DataTable();
            table = DB.ExecuteReader(sql);

            LOAICLS lcls = new LOAICLS();
            DataRow r = table.Rows[0];

            lcls.maloaicls = Int32.Parse(r[0].ToString());
            lcls.tenloai = r[1].ToString();

            return lcls;
        }

        public void updateTenLoaiWihID(int idLoaiCls, string tenLoai)
        {
            string sql = "update loaicls set tenloai = N'" + tenLoai + "' where maloai = " + idLoaiCls.ToString();
            DB.ExecuteNonQuery(sql);
        }
    }
}
